from django.test import TestCase
from django.test import RequestFactory, TestCase, Client
from rest_framework import status
from juqiadmin.models import JuqiAdmin
from warehouse.models import JuqiGood

import json

from pprint import pprint


# Create your tests here.
class JuqiGoodTest(TestCase):
    def setUp(self):
        # Every test needs access to the request factory.
        self.c = Client()
        self.factory = RequestFactory()
        self.admin_user = {"user": "admin", "password": "admin123"}
        u = JuqiAdmin(**self.admin_user)
        u.save()

        # request as wx user
        self.wx_c = Client(HTTP_X_WX_OPENID="wx123")

    @classmethod
    def create_good(cls, client: Client, good_info):
        r = client.post(
            "/api/warehouse/good",
            good_info,
            content_type="application/json",
        )
        return r

    # python manage.py test wuxianshang.tests.JuqiGoodTest
    def test_query_single_good(self):
        # login
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)
        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        # create good
        good_info = {
            "name": "wanou1",
            "price": 10,
            "display_url": "http://1111",
            "game_type": "wuxianshang",
        }
        g = JuqiGood(**good_info)
        g.save()

        # get single good
        r = self.c.get(
            "/api/warehouse/good",
            {"name": "wanou1"},
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        res = json.loads(r.content)
        self.assertEqual(res["price"], good_info["price"])
        self.assertEqual(res["uuid"], str(g.uuid))

    def test_query_page_good(self):
        # login
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)
        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        # create good
        good_info = {
            "name": "wanou1",
            "price": 10,
            "display_url": "http://1111",
            "game_type": "wuxianshang",
        }
        g = JuqiGood(**good_info)
        g.save()

        # get single good
        r = self.c.get(
            "/api/warehouse/good",
            {"list": "1", "num": 10, "page": 1},
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        res = json.loads(r.content)
        self.assertEqual(res[0]["price"], good_info["price"])
        self.assertEqual(res[0]["uuid"], str(g.uuid))

        # get single good by query game_type
        r = self.c.get(
            "/api/warehouse/good",
            {"list": "1", "num": 10, "page": 1, "game_type": "wuxianshang"},
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        res = json.loads(r.content)
        self.assertEqual(res[0]["price"], good_info["price"])
        self.assertEqual(res[0]["uuid"], str(g.uuid))

        # get single good by query game_type
        r = self.c.get(
            "/api/warehouse/good",
            {"list": "1", "num": 10, "page": 1, "game_type": "xxxx"},
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        res = json.loads(r.content)
        self.assertEqual(res, [])

    def test_create_good(self):
        # login
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        # create good
        good_info = {
            "name": "wanou1",
            "price": 10,
            "display_url": "http://1111",
            "game_type": "wuxianshang",
        }

        r = self.c.post(
            "/api/warehouse/good",
            good_info,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_201_CREATED)

        res = json.loads(r.content)
        self.assertEqual(res["name"], good_info["name"])
        self.assertEqual(res["price"], good_info["price"])
        self.assertEqual(res["display_url"], good_info["display_url"])

    def test_create_good_without_login(self):
        # create good
        good_info = {
            "name": "wanou1",
            "price": 10,
            "display_url": "http://1111",
            "game_type": "wuxianshang",
        }

        r = self.c.post(
            "/api/warehouse/good",
            good_info,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_400_BAD_REQUEST)
